//
//  UserCenterVC.m
//  ProjectforHDX
//
//  Created by Jaesun on 16/7/5.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "UserCenterVC.h"

#import "UserCenterHeadView.h" // 表头

#import "FeedBackVC.h" // 意见反馈
#import "ConsigneeListVC.h" // 收货联系人
#import "RemindSettingVC.h" // 提醒设置
#import "ChangePWDVC.h" // 修改密码
#import "StockInquiryVC.h" // 库存查询
#import "MessageVC.h"   // 我的消息
#import "CMyOrderVC.h" //我的订单
#import "CMyOrderCenterDetailCheck.h" //物流详情
#import "CNewOrderFaultyPartsVC.h"  //新建订单－选择故障件




@interface UserCenterVC ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate, UIActionSheetDelegate,MWPhotoBrowserDelegate> {
    /**
     *  用户图像
     */
    MWPhoto *userPhoto;
    /**
     *  用户信息Model
     */
    THUserInfoModel *userModel;
    
    BOOL hiden;
    
}

/**
 * 表
 */
@property (nonatomic, strong) UITableView *tableView;

/**
 *  表头
 */
@property (nonatomic, strong) UserCenterHeadView *headView;

@property (nonatomic, strong) RZCustomLable *languageLab;

/**
 *  提交的图片路径数组
 */
@property (nonatomic, strong) NSMutableArray *postFilePaths;
/**
 *  提交数据
 */
@property (nonatomic, strong) NSMutableDictionary *postDict;

@end

@implementation UserCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupDatas];
    [self setupViews];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    hiden = NO;
    [self.navigationController setNavigationBarHidden:YES animated:animated];
 
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    if (!hiden) {
        [self.navigationController setNavigationBarHidden:NO animated:animated];
        self.leftItem.hidden = YES;
    }
  
}

#pragma mark 数据初始化
- (void)setupDatas {
    
    userModel = [THMangerTool getAccountInfo];
    
    self.postDict = [NSMutableDictionary dictionary];
}
 

#pragma mark 视图初始化
- (void)setupViews {

    // view
    self.mBackColor = [UIColor groupTableViewBackgroundColor];
    
    // tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, - 20, SCREEN_WIDTH, SCREEN_HEIGHT + 20) style:(UITableViewStyleGrouped)];
    self.tableView.bounces=NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    // headView
    self.headView = [[UserCenterHeadView alloc] initWithFrame:CGRectMake(0, -20, BOUNDSWIDTH, 185)];
    self.headView.nameLabel.text = [NSString stringWithFormat:@"%@",userModel.realName];
    
    self.headView.backgroundColor = SYSTEM_COLOR;
    // 用户图像
    self.headView.photoView.userInteractionEnabled = YES;
    
    self.headView.photoView.image = [UIImage imageWithData:userModel.imageData];
    
    // [self.headView.photoView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?v=%@",userModel.headImage,[THMangerTool setUpHeadDateTime:NO]]] placeholderImage:BDFILEIMAGENAEM(@"UserCenter/photoHolder")];
 
    // userPhoto = [MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",userModel.headImage]]];
    
    userPhoto = [MWPhoto photoWithImage:self.headView.photoView.image];
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] init];
    [gesture addTarget:self action:@selector(pickImageAction:)];
    [self.headView.photoView addGestureRecognizer:gesture];
    [self.headView.leftBtn addTarget:self action:@selector(leftItem:) forControlEvents:UIControlEventTouchUpInside];
 
    self.tableView.tableHeaderView = self.headView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview: self.tableView];

}


 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark- TableViewDelegate DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  5;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    else if (section == 1) {
        THUserInfoModel *userModels=[THMangerTool getAccountInfo];
        
        NSLog(@"%@",userModels.userType);
        
        if ([userModels.userType integerValue] == 1){
            return 2;
        }else{
            return 2;

        }
        
    }
    else if (section == 2) {
#pragma mark appStore Test Account
        if ([Toolobject isAppStoreTestAccount]) {
            return 3;
        }
        return 4;
    }
    else if (section == 3) {
        return 1;
    }
    else if (section == 4) {
        return 1;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.font = CustomFont(15);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {  cell.imageView.image = BDFILEIMAGENAEM(@"UserCenter/order");
                cell.textLabel.text = [UtilCheck language:@"setting.order"];
                break;
            }
            default:
                break;
        }
    }
    else if(indexPath.section == 1) {
        switch (indexPath.row) {
                
            case 0: {
                cell.imageView.image = BDFILEIMAGENAEM(@"UserCenter/message");
                cell.textLabel.text = [UtilCheck language:@"setting.message"]; // 消息
                break;
            }
            case 1: {
                int flag = 0;
                for (int i = 0 ;i < userModel.operationPermissions.count ;i ++ ) {
                    NSInteger type=[userModel.operationPermissions[i] intValue];
                    if (type == 3) {
                        flag = -1;
                    }
                    
                }
                //如果没有库存查询的权限，就不显示库存查询cell
                if (flag == -1) {
                    cell.imageView.image = BDFILEIMAGENAEM(@"UserCenter/stockinquiry");
                    cell.textLabel.text = [UtilCheck language:@"setting.stockInquiry"]; // 库存查询
                }
                else
                {
                    cell.imageView.image = BDFILEIMAGENAEM(@"UserCenter/consignee");
                    cell.textLabel.text = [UtilCheck language:@"setting.consignee"]; // 收货联系人
                }
                
                break;
            }
            default:
                break;
        }
    }
    else if (indexPath.section == 2) {
        
        switch (indexPath.row) {
            case 0: {
                cell.imageView.image = BDFILEIMAGENAEM(@"UserCenter/remind");
                cell.textLabel.text = [UtilCheck language:@"setting.remind"]; // 密码管理
            }
                break;
            case 1:
            {
                cell.imageView.image = BDFILEIMAGENAEM(@"UserCenter/pwdmanager");
                cell.textLabel.text = [UtilCheck language:@"setting.pwdManager"]; // 密码管理
                break;
            }
            case 2:
            {
                CGFloat languageLabWidth = 80;
                CGFloat trailPadding = 30;
//                if (iPhone6Plus == YES) {
//                    languageLabWidth *= 1.15;
//                    trailPadding *= 1.15;
//                }
                cell.imageView.image = BDFILEIMAGENAEM(@"UserCenter/language");
                self.languageLab = [[RZCustomLable alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - languageLabWidth - trailPadding, 0, languageLabWidth, cell.height)];
                self.languageLab.textColor = [UIColor lightGrayColor];
                self.languageLab.font  = CustomFont(14);
                self.languageLab.textAlignment = NSTextAlignmentRight;
                if([THMangerTool getLanageType] == NSLanguageEnglish){
                      self.languageLab.text = @"Engish";
                }
                else {
                      self.languageLab.text = @"中文(简体)";
                }
                
                [cell addSubview:self.languageLab];
                cell.textLabel.text = [UtilCheck language:@"setting.lang"]; // 语言设置
                break;
            }
            case 3:
            {
                cell.imageView.image = BDFILEIMAGENAEM(@"UserCenter/version");
                [cell addSubview:[self setupVersionView]];
                cell.textLabel.text = [UtilCheck language:@"setting.version"]; // 版本
                break;
            }
            default:
                break;
        }
    }
   else if (indexPath.section == 3) {
        
        switch (indexPath.row) {
            case 0:
            {  cell.imageView.image = BDFILEIMAGENAEM(@"UserCenter/feedback");
                cell.textLabel.text = [UtilCheck language:@"setting.feedBack"]; // 意见反馈
                break;
            }
            default:
                break;
        }
    }
    if (indexPath.section == 4) {
        
        switch (indexPath.row) {
            case 0:
            {
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
                cell.textLabel.text = [UtilCheck language:@"setting.logout"]; // 退出当前账号
                break;
            }
            default:
                break;
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 15.f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0: {
                CMyOrderVC *vc = [[CMyOrderVC alloc]init];
                
                [self.navigationController pushViewController:vc animated:YES];
           }
            default:
                break;
        }
    }
    else if(indexPath.section == 1){
        switch (indexPath.row) {
//            case 0:{
//                hiden = YES;
//                StockInquiryVC *vc = [[StockInquiryVC alloc] init];
//                [self.navigationController pushViewController:vc animated:YES];
//
//            }
//                break;
            case 0:{
                MessageVC *vc = [[MessageVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                }
                break;
            case 1:{
                
                if ([userModel.userType isEqualToString:@"2"] || [userModel.userType isEqualToString:@"3"]||[userModel.userType isEqualToString:@"0"]) {
                    hiden = YES;
                    AppDelegate * app = [UIApplication sharedApplication].delegate;
                    app.shouldChangeOrientation = YES;

                    StockInquiryVC *vc = [[StockInquiryVC alloc] init];
                    [self presentViewController:vc animated:YES completion:nil];
                }
                else
                {
                    ConsigneeListVC *vc = [[ConsigneeListVC alloc] init];
                    [self.navigationController pushViewController:vc animated:YES];
                }
               
            }
                break;
            default:
                break;
        }
    }
    else if(indexPath.section == 2){
        switch (indexPath.row) {
                
            case 0: {
                // 跳转到当前应用的设置
                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    [[UIApplication sharedApplication] openURL:url];
                }
            }
                break;
            case 1:{
                ChangePWDVC *vc = [[ChangePWDVC alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 2:{
#pragma mark 语言设置
                UIAlertController *alertController=[UIAlertController alertControllerWithTitle:[UtilCheck language:@"setting.language"] message:nil preferredStyle:UIAlertControllerStyleAlert]  ;
    
                UIAlertAction *actionAlbum = [UIAlertAction actionWithTitle:[UtilCheck language:@"setting.chinese"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    if([THMangerTool getLanageType]!=NSLanguageChineese){
                        
                        [[BDHUD sharedManager] showBDHUD:self.view title:[UtilCheck language:@"setting.set"]];
                        
                        [UtilCheck setLanguage:NSLanguageChineese];
                        
                        [self performSelector:@selector(hideHUD) withObject:self afterDelay:3];
                    }
                    
                }];
                [alertController addAction:actionAlbum];
                
                UIAlertAction *actionAlbum1 = [UIAlertAction actionWithTitle:[UtilCheck language:@"setting.english"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    if([THMangerTool getLanageType]!=NSLanguageEnglish){
                        [[BDHUD sharedManager] showBDHUD:self.view title:[UtilCheck language:@"setting.set"]];
                        
                        [UtilCheck setLanguage:NSLanguageEnglish];
                        [self performSelector:@selector(hideHUD) withObject:self afterDelay:3];
                    }
                }];
                [alertController addAction:actionAlbum1];
                [self presentViewController:alertController animated:YES completion:nil];
        
            }
                break;
            case 3:{
                [self touchFindVersionBtn:nil];
            }
                break;
            default:
                break;
        }
    }
    else if(indexPath.section == 3){
        switch (indexPath.row) {
            case 0:{
                FeedBackVC *vc =  [[FeedBackVC alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            default:
                break;
        }
    }

    else {
        [self touchLogout];
     }
}

-(void)hideHUD{
    
    [[BDHUD sharedManager] dismissHUD];
    
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
        self.view.alpha=0.5;
        
    [self.navigationController popToRootViewControllerAnimated:YES];
    } completion:^(BOOL finished) {

        [APPDELEGATESelf setLanagne];
    }];
}

#pragma mark- imagePickControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSLog(@"get the media info: %@", info);
    
    UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    image = [Toolobject fixOrientation:image];
    
    userPhoto = [MWPhoto photoWithImage:image];
   
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    self.headView.photoView.image = image;
    

    // 修改用户头像
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //判断MyImageFolder文件夹是否存在
    BOOL fileExists = [fileManager fileExistsAtPath:UPLOAD(@"")];
    if(!fileExists)
    {
        [fileManager createDirectoryAtPath:UPLOAD(@"") withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *fileOldPath=[NSString stringWithFormat:@"%@/DT_%@.jpg",UPLOAD(@""),[UtilCheck DateToTimestamp]];

    //把图片数据写入沙盒目录
    NSData *ImageData = UIImageJPEGRepresentation(image, 0.10);  //jpg 压缩读取
 
    userModel.imageData = ImageData;
   
    [ImageData writeToFile:fileOldPath atomically:NO]; //写入文件
    

    self.postFilePaths = [NSMutableArray arrayWithObjects: fileOldPath, nil];
    // 上传个人图片
    [self postImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - MWPhotoBrowser协议
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    
    return  1;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    
   return userPhoto;
}

#pragma mark- 网络请求
#pragma mark-

#pragma mark 修改用户头像
- (void)postImage {
    {
        [[BDHUD sharedManager] showBDUploadHUD:self.view title:nil];
        NSMutableDictionary *dic =[NSMutableDictionary dictionary];
        [dic setObject:userModel.key forKey:@"key"];
        [dic setObject:userModel.userID forKey:@"userId"];
        [AFHttpTool postFilewithUrl:@"uploadHeadImgForApp" parameters:dic file:self.postFilePaths success:^(id responseObject) {
            [[BDHUD sharedManager] dismissHUD];
            if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
                [THMangerTool setUpHeadDateTime:YES];
           
                [[[UIImageView alloc] init] setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?v=%@",userModel.headImage,[THMangerTool setUpHeadDateTime:NO]]] placeholderImage:BDFILEIMAGENAEM(@"UserCenter/photoHolder")];
               

                [THMangerTool saveAccountModel:userModel];
                
//              [self postInfo];

         }
         else {
             //返回数据出问题 弹出服务器返回信息
             if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                 [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
             }
             else {
                 [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
             }
 
         }
            
        } progress:^(NSProgress *uploadProgress) {
            [[BDHUD sharedManager] setBDUploadProgressHUD:(CGFloat)uploadProgress.fractionCompleted];
        } fail:^(id responseObject, NSError *error) {
            [[BDHUD sharedManager] dismissHUD];
            //返回数据出问题 弹出服务器返回信息
            if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
                [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
            }else {
                [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
            }

        }];
        
    }
}

/*
 *
 *  提交反馈
 */
-(void)postInfo{
    

 
    [self.postDict setObject:userModel.realName forKey:@"realName"];
    [self.postDict setObject:userModel.headImage forKey:@"headImage"];
    // 指示器
   
    [[BDHUD sharedManager] showBDHUD:self.view title:[THMangerTool language:@"HUD.title.update"]];
    [AFHttpTool postJsonDatawithUrl:@"phone1_0!setUserInfo.action" parameters:self.postDict success:^(id responseObject) {
        
        [[BDHUD sharedManager] dismissHUD];
        
        if(1 == [[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]) {
            
            //返回数据出问题 弹出服务器返回信息
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]) {
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            }
            else {
                [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.title.head.portrait.update"]];
            }
        }
        else {
            //返回数据出问题 弹出服务器返回信息
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            }
            else {
                [[BDHUD sharedManager] showWords:[UtilCheck language:@"BDHUD.ServersDataError"]];
            }
        }
    } fail:^(id responseObject, NSError *error) {
        [[BDHUD sharedManager] dismissHUD];
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
    }];
}

#pragma mark- 其他事件
#pragma mark-

#pragma mark 设置发现新版本cell
- (UIView *)setupVersionView {
    
    CGFloat viewWidth = 130;
    CGFloat trailPadding = 30;
   
//    if (iPhone6Plus==YES) {
//        viewWidth *= 1.15;
//        trailPadding *= 1.15;
//    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - viewWidth - trailPadding, 11, viewWidth, 20)];
    RZCustomButtom *findVersionBtn = [RZCustomButtom buttonWithType:(UIButtonTypeCustom)];
    [findVersionBtn setTitle:[UtilCheck language:@"setting.findVersion"] forState:(UIControlStateNormal)]; // 发现新版本
    findVersionBtn.titleLabel.font = CustomFont(12);
    findVersionBtn.layer.cornerRadius = 8.5;
    findVersionBtn.layer.masksToBounds = YES;
    findVersionBtn.backgroundColor = [UtilCheck hexStringToColor:@"fb5436" alpha:1];
    findVersionBtn.frame = CGRectMake(0, 1.5, view.width / 2 + 15, view.height - 3);
    [view addSubview:findVersionBtn];
    RZCustomLable *versionInfoLabel = [[RZCustomLable alloc] initWithFrame:CGRectMake(findVersionBtn.right, findVersionBtn.top, view.width / 2 - 15, findVersionBtn.height)];
    [view addSubview:versionInfoLabel];
    versionInfoLabel.text = [NSString stringWithFormat:@"V %@",[THMangerTool getVersion:YES]];
    versionInfoLabel.font = CustomFont(14);
    versionInfoLabel.textColor = [THMangerTool getRZColor:171 green:171 blue:171 alpha:1];
    versionInfoLabel.textAlignment = NSTextAlignmentCenter;
    return view;
}
#pragma mark 返回
- (void)touchLeftBtn:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 退出登录

- (void)touchLogout {
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:[UtilCheck language:@"setting.logout"] message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *logoutAction = [UIAlertAction actionWithTitle:[UtilCheck language:@"setting.logout"] style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        NSMutableArray *arr = [NSMutableArray array];
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:arr forKey:@"getSearcHistoryKey"];
        [userDefaults synchronize];
        [THMangerTool removeAccount];
        [APPDELEGATESelf setUpMainViewControllerOrLoginViewController:NO];
    }];
    
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:[UtilCheck language:@"setting.cancel"] style:(UIAlertActionStyleCancel) handler:nil];
    [alertC addAction:logoutAction];
    [alertC addAction:cancleAction];
    if ([[UIDevice currentDevice].model isEqualToString:@"iPad"]) {
        alertC.popoverPresentationController.sourceView = self.view;
        alertC.popoverPresentationController.sourceRect = CGRectMake((self.view.frame.size.width-200)/2,self.view.frame.size.height,200,1.0);
    }
    [self presentViewController:alertC animated:YES completion:nil];
    
}

#pragma mark 发现新版本
- (void)touchFindVersionBtn:(UIButton *)sender {
    
    [[BDHUD sharedManager] showBDHUD:self.view title:nil];
    
    [AFHttpTool postJsonDatawithUrl:@"appUpdate" parameters:nil
                            success:^(id responseObject) {
                                [[BDHUD sharedManager] dismissHUD];
                               
                                if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
                                    NSDictionary *dataDict = [[responseObject objectForKey:@"data"] mutableCopy];
                                    
                                    NSLog(@"dataDict: %@",dataDict);
                                    NSLog(@"服务器的版本号是%@",[dataDict objectForKey:@"updateVersionName"]);
                                    NSLog(@"当前程序的版本号是%@",[HDXMangerTool getVersion]);
                                    
                                    if ([[dataDict objectForKey:@"updateVersionName"] isEqualToString: [HDXMangerTool getVersion]]) {
                                        [[BDHUD sharedManager] showWords:[UtilCheck language:@"BDHUD.isLatestVersion"]];
                                        return;
                                    }
                                    if ([dataDict objectForKey:@"forced"]!= nil && [[dataDict objectForKey:@"forced"] integerValue] == 0) {//非强制更新
                                        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:[UtilCheck language:@"version.update"] message:
                                                                     [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"updateContent"]]  preferredStyle:(UIAlertControllerStyleAlert)];
                                        
                                        UIAlertAction *logoutAction = [UIAlertAction actionWithTitle:[UtilCheck language:@"button.cancel"] style:(UIAlertActionStyleCancel) handler:nil];
                                        
                                        UIAlertAction *updateAction = [UIAlertAction actionWithTitle:[UtilCheck language:@"button.done"] style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                                            
                                            NSString *updateURLStr = [dataDict objectForKey:@"updateUrl"];
                                            NSURL *updateURL = [NSURL URLWithString:updateURLStr];
                                            
                                            if ([[UIApplication sharedApplication] canOpenURL:updateURL]) {
                                                //前往应用商店
                                                [[UIApplication sharedApplication] openURL:updateURL];
                                            }
                                            else {
                                                //前往应用商店
                                                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:KEY_APPSTORE_URL]];
                                            }
                                            
                                        }];
                                        [alertC addAction:logoutAction];
                                        [alertC addAction:updateAction];
                                        [self presentViewController:alertC animated:YES completion:nil];
                                    } else if ([dataDict objectForKey:@"forced"]!= nil && [[dataDict objectForKey:@"forced"] integerValue] == 1){//强制更新
                                        
                                        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:[THMangerTool language:@"version.update"] message:[NSString stringWithFormat:@"%@",[dataDict objectForKey:@"updateContent"]] preferredStyle:(UIAlertControllerStyleAlert)];
                                        
                                        UIAlertAction *logoutAction = [UIAlertAction actionWithTitle:[UtilCheck language:@"button.cancel"] style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                                            [THMangerTool removeAccount];
                                            [APPDELEGATESelf setUpMainViewControllerOrLoginViewController:NO];
                                        }];
                                        
                                        UIAlertAction *updateAction = [UIAlertAction actionWithTitle:[UtilCheck language:@"button.done"] style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                                            //前往应用商店
                                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:KEY_APPSTORE_URL]];
                                        }];
                                        
                                        [alertC addAction:logoutAction];
                                        [alertC addAction:updateAction];
                                        [self presentViewController:alertC animated:YES completion:nil];
                                    }
                                    
                                    
                            
                                }
                                else{
                                    
                                }
                            } fail:^(id responseObject, NSError *error) {
                                [[BDHUD sharedManager] dismissHUD];
                                
                                NSLog(@"获取更新数据失败");
                                
                            }];
    
}

#pragma mark 点击图片
- (void)pickImageAction:(UIGestureRecognizer *)sender
{
    
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:[UtilCheck language:@"setting.changePhoto"] message:nil preferredStyle:UIAlertControllerStyleAlert]  ;
    
    UIAlertAction *actionAlbum = [UIAlertAction actionWithTitle:[UtilCheck language:@"title.photoAlbum"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
        {
            
            UIImagePickerController *Imagepicker = [[UIImagePickerController alloc] init];
            Imagepicker.delegate =self;
 
            Imagepicker.allowsEditing = YES;
            Imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:Imagepicker animated:YES completion:nil];
            
        }
        else{
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"title.noAlbum"]];
        }
        
    }];
    [alertController addAction:actionAlbum];
    
    UIAlertAction *actionCamera = [UIAlertAction actionWithTitle:[UtilCheck language:@"title.takePhotos"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            
            UIImagePickerController *picker =[[UIImagePickerController alloc] init];
            picker.delegate = self;
                        picker.allowsEditing = YES;
            
            picker.sourceType =  UIImagePickerControllerSourceTypeCamera;
            
            [self presentViewController:picker animated:YES
                             completion:^{
                                 
                             }];
        }
        else{
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"title.noCamera"]];
        }
        
    }];
    [alertController addAction:actionCamera];
    
    if(userPhoto) {
        UIAlertAction *actionLook = [UIAlertAction actionWithTitle:[UtilCheck language:@"title.largerImage"]style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
            
            browser.displayActionButton = NO;//分享按钮,默认是
            browser.displayNavArrows = NO;//左右分页切换,默认否
            browser.displaySelectionButtons = NO;//是否显示选择按钮在图片上,默认否
            browser.alwaysShowControls = NO;//底部 顶部控制条件控件 是否显示,默认否
            browser.zoomPhotosToFill = YES;//是否全屏,默认是
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
            browser.wantsFullScreenLayout = YES;//是否全屏
#endif
            browser.enableGrid = NO;//是否允许用网格查看所有图片,默认是
            browser.startOnGrid = YES;//是否第一张,默认否
            browser.enableSwipeToDismiss = YES;
            
            
            [browser setCurrentPhotoIndex:0];
            
            
            CustomNavigationController *navCon = [[CustomNavigationController alloc] initWithRootViewController:browser];
            
            [self presentViewController:navCon animated:YES completion:nil];
            
        }];
        [alertController addAction:actionLook];
    }

    UIAlertAction *action1 = [UIAlertAction actionWithTitle:[UtilCheck language:@"title.cancel"] style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alertController addAction:action1];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

@end
